--[[控制canvas是否绘制
1.canvas显示的条件。
2.canvas显示的次序。
3.canvas不显示。
]]
require "libraries.tiledmap.tiledmap"
require "libraries.third-party.class" --摇杆需要引入的


gKeyPressed = {}
gCamX,gCamY = 100,100

function loadMap(path)
  TiledMap_Load(path)
  gCamX,gCamY = gMapWidth * gTileWidth / 2, gMapHeight * gTileHeight / 2
end

function love.load()

	love.graphics.setBackgroundColor(1,1,1)
	font = love.graphics.newFont("resources/fonts/SourceHanSansCN-Normal.otf",12)
	love.graphics.setFont(font)
    --画布的显示次序，用一个Table记录
    canvas_z_index={"canvas1","canvas2","canvas3"}
    --画布的属性
    canvas1property ={posx=50,posy=50,width=100,height=200,visible=false}
    canvas2property ={posx=100,posy=100,width=100,height=200,visible=false}
    canvas3property ={posx=150,posy=150,width=100,height=200,visible=false}
	canvas1button1property={posx=50,posy=50,width=100,height=200,visible=false}
	
	--新建了画布0
    canvas0 = love.graphics.newCanvas(800, 600)
    love.graphics.setCanvas(canvas0)
        love.graphics.clear()
        love.graphics.setColor(1, 0, 0)
        love.graphics.rectangle('fill', 0, 0, 100, 200)
        love.graphics.setColor(0, 0, 0)
        love.graphics.rectangle('fill', 0, 0, 100, 20) --标题栏
        love.graphics.setColor(1, 1, 1)
        love.graphics.print("canvas0",10,2)
        --绘制图片
        --love.graphics.setBackgroundColor(1,1,1)
        image1 = love.graphics.newImage( "resources/images/scenario/default/persons/F01.png" )
        love.graphics.draw(image1,30,30)
	love.graphics.setCanvas() 
	
	--新建了画布1
    canvas1 = love.graphics.newCanvas(800, 600)
    love.graphics.setCanvas(canvas1)
        love.graphics.clear()
        love.graphics.setColor(192/255, 192/255, 192/255)
        love.graphics.rectangle('fill', 0, 0, 100, 200)
        love.graphics.setColor(0, 0, 0)
        love.graphics.rectangle('fill', 0, 0, 100, 20) --标题栏
        love.graphics.rectangle('fill', 0, 180, 100, 20) --关闭
        love.graphics.setColor(1, 1, 1)
        love.graphics.print("人物属性",10,2)
        love.graphics.print("关闭",10,182)
        --绘制图片
        --love.graphics.setBackgroundColor(1,1,1)
        image1 = love.graphics.newImage( "resources/images/scenario/default/persons/F01.png" )
        love.graphics.draw(image1,20,30)
        love.graphics.setColor(0, 0, 0)
        love.graphics.print("姓名：王雪红",20,94)
        love.graphics.print("性别：女",20,114)
        love.graphics.print("财富：800亿",20,134)
	love.graphics.setCanvas()
	 --新建了画布2
	 canvas2 = love.graphics.newCanvas(800, 600)
	 love.graphics.setCanvas(canvas2)
		 love.graphics.clear()
		 love.graphics.setColor(0, 1, 0)
		 love.graphics.rectangle('fill', 0, 0, 100, 200)
		 love.graphics.setColor(0, 0, 0)
		 love.graphics.rectangle('fill', 0, 0, 100, 20) --标题栏
		 love.graphics.setColor(1, 1, 1)
         love.graphics.print("建筑属性",10,2)
	 love.graphics.setCanvas()
	--新建了画布3
	canvas3 = love.graphics.newCanvas(800, 600)
	love.graphics.setCanvas(canvas3)
		love.graphics.clear()
		love.graphics.setColor(0, 0, 1)
		love.graphics.rectangle('fill', 0, 0, 100, 200)
		love.graphics.setColor(0, 0, 0)
		love.graphics.rectangle('fill', 0, 0, 100, 20) --标题栏
		love.graphics.setColor(1, 1, 1)
        love.graphics.print("财务报表",10,2)

	love.graphics.setCanvas()

--载入地图
	loadMap("map/map01.tmx")
  
--绘制摇杆
    bg = love.graphics.newImage("resources/images/layout.png")
	center = love.graphics.newImage("resources/images/center.png")
	hero = love.graphics.newImage("resources/images/aircraft-ne.png")	
	gr=love.graphics
	point = {x=200,y=200}	
	joy = require "libraries/third-party/_joystick"(bg,center)
	joy:setPoint(130,love.graphics.getHeight()-130)	
	--其他
	var = 10   -- arbitrary starting value
    rate = 3   -- change to change the rate at which the var is changed
end

function love.keyreleased( key )
	gKeyPressed[key] = nil
end

function love.keypressed( key, unicode ) 
	--地图
	gKeyPressed[key] = true 
	if (key == "escape") then os.exit(0) end
	if (key == " ") then -- space = next mal
		gMapNum = (gMapNum or 1) + 1
		if (gMapNum > 3) then gMapNum = 1 end
		loadMap(string.format("map/map%02d.tmx",gMapNum))
	end
	--未分类
	if key == "escape" then
		love.event.quit()
	end
	if love.keyboard.isDown("down") then   -- reduce the value
		var = var -  rate
	end
	if love.keyboard.isDown("up") then   -- increase the value
		var = var + rate
	end
end

function love.update( dt )
	var = var + (dt * rate)
    --代码分割
	local s = 500*dt
	if (gKeyPressed.up) then gCamY = gCamY - s end
	if (gKeyPressed.down) then gCamY = gCamY + s end
	if (gKeyPressed.left) then gCamX = gCamX - s end
	if (gKeyPressed.right) then gCamX = gCamX + s end
  -- Keep camera within map bounds
  gCamX = math.min(math.max(gCamX, 0), gMapWidth * gTileWidth)
  gCamY = math.min(math.max(gCamY, 0), gMapHeight * gTileHeight)
  
     --摇杆更新
  if joy.dir == 1 then 		
	elseif joy.dir == 2 then 
		point.y = point.y + 300*dt * joy.power
		gCamY = gCamY + 300*dt * joy.power
	elseif joy.dir == 3 then 
	elseif joy.dir == 4 then 
		point.x = point.x - 300*dt
		gCamX = gCamX - 300*dt
	elseif joy.dir == 6 then 
		point.x = point.x + 300*dt
		gCamX = gCamX + 300*dt
	elseif joy.dir == 7 then 
	elseif joy.dir == 8 then 
		point.y = point.y - 300*dt
		gCamY = gCamY - 300*dt
	elseif joy.dir == 9 then 
	end
end

function love.draw()
--先画地图
    love.graphics.print('arrow-keys=scroll, space=next map', 50, 50)
	love.graphics.setBackgroundColor(80/255,80/255,80/255)
	TiledMap_DrawNearCam(gCamX,gCamY) 
    --middleclass测试区
    local class = require 'libraries.middleclass.middleclass'
    local Fruit = class('Fruit') -- 'Fruit' is the class' name   
    function Fruit:initialize(sweetness)
      self.sweetness = sweetness
    end    
    Fruit.static.sweetness_threshold = 5 -- class variable (also admits methods)   
    function Fruit:isSweet()
      return self.sweetness > Fruit.sweetness_threshold
    end    
    local Lemon = class('Lemon', Fruit) -- subclassing   
    function Lemon:initialize()
      Fruit.initialize(self, 1) -- invoking the superclass' initializer
    end    
    local lemon = Lemon:new() 
    love.graphics.setColor(0, 0, 0)
    love.graphics.print(tostring(lemon:isSweet()),600,20)
    --middleclass测试区
--graphics直接画按钮
    --love.graphics.setColor(192/255, 192/255, 192/255)
    love.graphics.setColor(1, 0, 0)
    love.graphics.rectangle('fill', 0, 0, 60, 60)  
    love.graphics.setColor(0, 1, 0)
    love.graphics.rectangle('fill', 120, 0, 60, 60) 
    love.graphics.setColor(0, 0, 1)
    love.graphics.rectangle('fill', 240, 0, 60, 60)  
    love.graphics.setColor(0, 0, 0)
    love.graphics.print("人物属性",10,10)
    love.graphics.print("建筑属性",130,10)
    love.graphics.print("财务报表",250,10)
    love.graphics.setColor(0, 0, 0)
    love.graphics.print(tostring(canvas_z_index[3]),10,110)   
    love.graphics.print(tostring(canvas_z_index[2]),10,150)   
    love.graphics.print(tostring(canvas_z_index[1]),10,190)   
    love.graphics.print("canvas1.visible="..tostring(canvas1property.visible),10,230)   
    love.graphics.print("canvas2.visible="..tostring(canvas2property.visible),10,270)   
	love.graphics.print("canvas3.visible="..tostring(canvas3property.visible),10,310)
--控制canvas是否显示
		love.graphics.setColor(1, 1, 1)
		local x, y = love.mouse.getPosition() -- get the position of the mouse
		i=1
		::s0::  
		if canvas_z_index[i]=="canvas1" then
			i=i+1
			goto s1
		elseif canvas_z_index[i]=="canvas2"  then
			i=i+1       
			goto s2
		elseif canvas_z_index[i]=="canvas3"  then
			i=i+1       
			goto s3
		else
			goto s4
		end
	
		::s1:: 
		if canvas1property.visible == true then
			love.graphics.draw(canvas1, canvas1property.posx, canvas1property.posy)    
		end
		goto s0
		::s2::
		if canvas2property.visible == true then
			love.graphics.draw(canvas2, canvas2property.posx, canvas2property.posy)
		end
		goto s0
		::s3::
		if canvas3property.visible == true then
			love.graphics.draw(canvas3, canvas3property.posx, canvas3property.posy)
		end
		goto s0
		::s4::
--摇杆
 
  joy:draw()--摇杆
end
function love.mousepressed(x, y, button, istouch)
	--摇杆
	joy:touchpressed(1,x,y)
	--画布控制
    local moux= love.mouse.getX( )
    local mouy=love.mouse.getY( )
    if moux>0 and moux <60 and mousepressedcanvas=="graphics" then
        if mouy>0 and mouy<60 then
            if love.mouse.isDown(1) then
                for i=1,3,1 do
                   if  canvas_z_index[i]=="canvas1" then    
                   table.remove(canvas_z_index, i)
                   end
                end
                table.insert(canvas_z_index, 3, "canvas1")
                canvas1property.visible=not(canvas1property.visible)  
            end
        end
    end
    if moux>120 and moux <180 and mousepressedcanvas=="graphics" then
        if mouy>0 and mouy<60 then
            if love.mouse.isDown(1) then
                for i=1,3,1 do
                    if  canvas_z_index[i]=="canvas2" then    
                    table.remove(canvas_z_index, i)
                    end
                 end
                 table.insert(canvas_z_index, 3, "canvas2")
                canvas2property.visible=not(canvas2property.visible)  
            end
        end
    end
    if moux>240 and moux <300 and mousepressedcanvas=="graphics" then
        if mouy>0 and mouy<60 then
            if love.mouse.isDown(1) then
                for i=1,3,1 do
                    if  canvas_z_index[i]=="canvas3" then    
                    table.remove(canvas_z_index, i)
                    end
                 end
                 table.insert(canvas_z_index, 3, "canvas3")
                canvas3property.visible=not(canvas3property.visible)  
            end
        end
    end     
end
function love.mousemoved( x, y, dx, dy, istouch)    --鼠标捕捉
	--摇杆
	joy:touchmoved(1,x,y)

	local moux= love.mouse.getX( )
    local mouy=love.mouse.getY( )
    mousepressedcanvas="graphics" 
    --canvas1的移动
    if moux>canvas1property.posx and moux <canvas1property.posx+canvas1property.width then
        if mouy>canvas1property.posy and mouy<canvas1property.posy+canvas1property.height then
            if canvas1property.visible  then mousepressedcanvas="canvas1" end
            if love.mouse.isDown(1) and canvas1property.visible then             
             canvas1property.posx = canvas1property.posx+dx
             canvas1property.posy = canvas1property.posy+dy        
            end
        end
    end
    --canvas2的移动
    if moux>canvas2property.posx and moux <canvas2property.posx+canvas2property.width then
        if mouy>canvas2property.posy and mouy<canvas2property.posy+canvas2property.height then
            if canvas2property.visible  then mousepressedcanvas="canvas2" end--判断鼠标按的是哪一层canvas
             if love.mouse.isDown(1) and canvas2property.visible then
             canvas2property.posx = canvas2property.posx+dx
             canvas2property.posy = canvas2property.posy+dy        
            end
        end
    end
    --canvas3的移动
    if moux>canvas3property.posx and moux <canvas3property.posx+canvas3property.width then
        if mouy>canvas3property.posy and mouy<canvas3property.posy+20 then
            if canvas3property.visible  then mousepressedcanvas="canvas3" end--判断鼠标按的是哪一层canvas
             if love.mouse.isDown(1) and canvas3property.visible then
             canvas3property.posx = canvas3property.posx+dx
             canvas3property.posy = canvas3property.posy+dy        
            end
        end
    end
end
function love.mousereleased(x,y)
	joy:touchreleased(1,x,y)		
end
function love.touchpressed(...)
	joy:touchpressed(...)
end
function love.touchmoved(...)
	joy:touchmoved(...)
end
function love.touchreleased(...)
joy:touchreleased(...)
end